package com.wjh.kt_vod.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjh.kt_vod.exception.GgkException;
import com.wjh.kt_vod.listener.ExcelListener;
import com.wjh.kt_vod.mapper.SubjectMapper;
import com.wjh.kt_vod.model.vod.Subject;
import com.wjh.kt_vod.service.SubjectService;
import com.wjh.kt_vod.vo.vod.SubjectEeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
*
*/
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper,Subject>
implements SubjectService{

    @Autowired
    @Qualifier("excelListener")
    private ExcelListener excelListener;

    @Override
    public List<Subject> selectChildSubject(Long parent_id) {
        QueryWrapper<Subject> subjectQueryWrapper = new QueryWrapper<>();
        subjectQueryWrapper.eq("parent_id", parent_id);
        List<Subject> subjects = baseMapper.selectList(subjectQueryWrapper);
        for (Subject subject : subjects) {
            subject.setHasChildren(this.getChildExist(subject.getId()));
        }
        return subjects;
    }

    /**
     * 判断是否有下一层id
     * @param id
     * @return
     */
    @Override
    public boolean getChildExist(Long id){
        QueryWrapper<Subject> subjectQueryWrapper = new QueryWrapper<>();
        subjectQueryWrapper.eq("parent_id",id);
        Long aLong = baseMapper.selectCount(subjectQueryWrapper);
        return aLong>0;
    }

    @Override
    public void exportData(HttpServletResponse httpServletResponse) {

        try {
            httpServletResponse.setContentType("application/vnd.ms-excel");
            httpServletResponse.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("课程分类", "UTF-8");
            httpServletResponse.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");
            List<Subject> dictList = baseMapper.selectList(null);
            List<SubjectEeVo> dictVoList = new ArrayList<>(dictList.size());
            for(Subject dict : dictList) {
                SubjectEeVo dictVo = new SubjectEeVo();
                BeanUtils.copyProperties(dict,dictVo);
                dictVoList.add(dictVo);
            }
            EasyExcel.write(httpServletResponse.getOutputStream(), SubjectEeVo.class).sheet("课程分类").doWrite(dictVoList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void importData(MultipartFile multipartFile) {
        try {
            EasyExcel.read(multipartFile.getInputStream(),SubjectEeVo.class,excelListener )
            .sheet().doRead();
        } catch (IOException e) {
            throw new GgkException(20001,"导入失败");
        }
    }


}
